﻿#include "imageediter.h"

#include <QPaintEvent>
#include <QPainter>
#include <QApplication>
#include <QDesktopWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <qmath.h>
#include <QDebug>

using namespace Qt;

const static qreal ARROW_MINSIZE = 20.0;

void drawarrow(QPoint startpoint, QPoint endpoint, QPainter &p)
{
    int diffX = qAbs(endpoint.x() - startpoint.x());
    int diffY = qAbs(endpoint.y()- startpoint.y());

    qreal diff = qSqrt(diffX * diffX + diffY * diffY);
    if(diff < ARROW_MINSIZE){
        return;
    }

    double par = 15.0;
    double slopy = qAtan2((endpoint.y()-startpoint.y()),(endpoint.x()-startpoint.x()));
    double cosy= qCos(slopy);
    double siny= qSin(slopy);
    QPoint point1 = QPoint(endpoint.x() + int(-par*cosy - (par / 2.0*siny)), endpoint.y() + int(-par*siny + (par / 2.0*cosy)));
    QPoint point2 = QPoint(endpoint.x() + int(-par*cosy + (par / 2.0*siny)), endpoint.y() - int(par / 2.0*cosy + par*siny));
    QPoint points[3]={endpoint,point1,point2};
    p.setRenderHint(QPainter::Antialiasing,true);
    p.drawPolygon(points,3);

    int offsetx=int(par*siny/3);
    int offsety=int(par*cosy/3);
    QPoint point3,point4;
    point3 = QPoint(endpoint.x() + int(-par*cosy - (par / 2.0*siny)) + offsetx, endpoint.y() + int(-par*siny + (par / 2.0*cosy)) - offsety);
    point4 = QPoint(endpoint.x() + int(-par*cosy + (par / 2.0*siny) - offsetx), endpoint.y() - int(par / 2.0*cosy + par*siny) + offsety);
    QPoint arrbodypoints[3]={startpoint,point3,point4};
    p.drawPolygon(arrbodypoints,3);
}

void drawarrow2(QVector<QPoint> points,QPainter &p)
{
    if(points.size() < 2) {
        return;
    }

    //    int diffX = qAbs(endpoint.x() - startpoint.x());
    //    int diffY = qAbs(endpoint.y()- startpoint.y());

    //    qreal diff = qSqrt(diffX * diffX + diffY * diffY);
    //    if(diff < ARROW_MINSIZE){
    //        return;
    //    }

    for(int i=0;i<points.size()-1;++i){
        auto p1 = points.at(i);
        auto p2 = points.at(i+1);
        p.drawLine(p1,p2);
    }

    QPoint startpoint = points.at(points.size()-2);
    QPoint endpoint = points.at(points.size()-1);

    drawarrow(startpoint,endpoint,p);
}

void drawMosaic(const int& width, const QVector<MosaicInfo>& vec,QPainter &painter){
    for(const auto& info : vec){
        QRect rect = {0,0, width, width};
        QColor color = info.c;

        rect.moveCenter(QPoint(info.p.x()-1, info.p.y()-1));

        painter.fillRect(rect, color);
    }
}

QColor ImageEditer::getAvarageColor(const QPoint& pos)
{
    int width = getMosaicWidth();
    QRect rect = {0, 0, width, width};
    rect.moveCenter(QPoint(pos.x()-1, pos.y()-1));
    
    int sumR = 0, sumG = 0, sumB = 0, avarageR = 0, avarageG = 0, avarageB = 0;
    QColor temp;
    
    for (int k = rect.x(); k < rect.x() + width; k++)
    {
        for (int m = rect.y(); m < rect.y() + width; m++)
        {
            temp = selectimage.pixelColor(QPoint(k,m));
            sumR += temp.red();
            sumG += temp.green();
            sumB += temp.blue();
        }
    }
    
    int area = width *width;
    avarageR = sumR/area;
    avarageG = sumG/area;
    avarageB = sumB/area;

    QColor c = QColor(avarageR, avarageG, avarageB);
    
    return c;
}

ImageEditer::ImageEditer(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);//无边框
    setAttribute(Qt::WA_TranslucentBackground, true);

    setContextMenuPolicy(Qt::NoContextMenu);

    setMouseTracking(true);

    ispressed=false;
    isclip=false;
    isdrawline=false;
    isdrawrectangle=false;
    isdrawround=false;
    istextedit=false;
    isdrawarrow=false;
    isdrawarrow2=false;
    isdrawmosaic=false;
    isdrawface=false;

    clipConfirm = new QWidget(this);
    QHBoxLayout* clipLayout = new QHBoxLayout(clipConfirm);
    QPushButton* pbClipCancel = new QPushButton(clipConfirm);
    QPushButton* pbClipOk = new QPushButton(clipConfirm);

    pbClipCancel->setObjectName("pbClipCancel");
    pbClipOk->setObjectName("pbClipOk");
    pbClipCancel->setFixedSize(24,24);
    pbClipOk->setFixedSize(24,24);
    pbClipCancel->installEventFilter(this);
    pbClipOk->installEventFilter(this);
    connect(pbClipCancel,&QPushButton::clicked,[=](bool){
        isclip = false;
        clipRect = QRect();
        clipConfirm->hide();
        updateNow();
    });

    connect(pbClipOk,&QPushButton::clicked,[=](bool){
        if(isclip){
            doClip();
        }
        clipRect = QRect();
        clipConfirm->hide();
        updateNow();
    });

    clipLayout->addWidget(pbClipCancel);
    clipLayout->addWidget(pbClipOk);
    clipConfirm->setLayout(clipLayout);
    clipConfirm->setVisible(false);
    clipConfirm->setStyleSheet("#pbClipOk {border-image: url(:/image/20181018/queren.png);}"
                               "#pbClipCancel {border-image: url(:/image/20181018/quxiao.png);}"
                               "QWidget{"
                               "        border-radius: 4px;"
                               "        border: 1px solid rgb(200, 200, 200);"
                               "        background-color: rgb(255, 255, 255);}");

    m_plaintextedit=new QTextEdit(this);
    m_plaintextedit->setContextMenuPolicy(Qt::NoContextMenu);
    m_plaintextedit->hide();
    QPalette p1=m_plaintextedit->palette();
    p1.setBrush(QPalette::Base,QBrush(QColor(255,0,0,0)));
    m_plaintextedit->setContentsMargins(0,0,0,0);
    m_plaintextedit->setPalette(p1);
    m_plaintextedit->setStyleSheet("QTextEdit{ border: 1px solid #dadada;}");
    m_plaintextedit->installEventFilter(this);

    connect(m_plaintextedit,SIGNAL(textChanged()),this,SLOT(ontextchanged()));
}

void ImageEditer::ontextchanged()
{
    if(m_plaintextedit->toPlainText().size() == 0){
        m_plaintextedit->resize(100,50);
    }
    else
    {
       int factor = 15;
       int size = currentSize();

       if(size < 3){
           factor = 13;
       }else if(size > 3){
           factor = 18;
       }

       int w =  m_plaintextedit->toPlainText().size()*factor+40;
       int h =  m_plaintextedit->document()->size().rheight()+10;
       QPoint tfPoint = m_plaintextedit->geometry().topLeft();

       if(tfPoint.x() + w +2 > this->width()){
           w = this->width() - tfPoint.x() - 5;
       }

       if(tfPoint.y() + h + 2 > this->height()){
           h = this->height() - tfPoint.y() - 5;
       }

       m_plaintextedit->resize(w,h);
    }
}

void ImageEditer::mouseMoveEvent(QMouseEvent *event)
{
    if(isEditing())
    {
        setCursor(CrossCursor);
    }else if(ispressed) {
       // setCursor(OpenHandCursor);
    }

    if(ispressed && isEditing())
    {
        QPoint point = event->pos();
        if(isclip){
             endPoint=point;

             int sx = startPoint.x();
             int sy = startPoint.y();
             int ex = endPoint.x();
             int ey = endPoint.y();

             QPoint topLeft(qMin(sx,ex),qMin(sy,ey));
             QPoint bottomRight(qMax(sx,ex),qMax(sy,ey));

             clipRect = QRect(topLeft,bottomRight);
        }
        else if(isdrawline)
        {
            endPoint=point;
            auto itemPtr = items.at(items.size()-1);
            if(itemPtr && itemPtr->type() == IT_LINE){
                 auto linePtr = qSharedPointerCast<myLine>(itemPtr);
                 linePtr->path.lineTo(endPoint);
            }
            startPoint=endPoint;
        }
        else if(isdrawmosaic) {
             endPoint=point;

             int mosaicWidth = getMosaicWidth();

             double diffX = qAbs(endPoint.x() - mosaicLastPoint.x());
             double diffY = qAbs(endPoint.y()- mosaicLastPoint.y());

             double diff = qSqrt(diffX * diffX + diffY * diffY);

             if(diffX >= mosaicWidth){
                 if(endPoint.x() - mosaicLastPoint.x() >= 0){
                     while(diffX > mosaicWidth){
                         mosaicLastPoint.rx() += mosaicWidth;
                         diffX -= mosaicWidth;

                         MosaicInfo info = {mosaicLastPoint, getAvarageColor(mosaicLastPoint)};
                         mosaicPoints.push_back(info);
                     }

                     if(diffX != 0){
                         mosaicLastPoint.rx() += mosaicWidth;

                         MosaicInfo info = {mosaicLastPoint, getAvarageColor(mosaicLastPoint)};
                         mosaicPoints.push_back(info);
                     }
                 }else if(endPoint.x() - mosaicLastPoint.x() <= 0){
                     while(diffX > mosaicWidth){
                         mosaicLastPoint.rx() -= mosaicWidth;
                         diffX -= mosaicWidth;

                         MosaicInfo info = {mosaicLastPoint, getAvarageColor(mosaicLastPoint)};
                         mosaicPoints.push_back(info);
                     }

                     if(diffX != 0){
                         mosaicLastPoint.rx() -= mosaicWidth;

                         MosaicInfo info = {mosaicLastPoint, getAvarageColor(mosaicLastPoint)};
                         mosaicPoints.push_back(info);
                     }
                 }
             }

             if(diffY > mosaicWidth){
                 if(endPoint.y() - mosaicLastPoint.y() > 0){
                     while(diffY > mosaicWidth){
                         mosaicLastPoint.ry() += mosaicWidth;
                         diffY -= mosaicWidth;

                         MosaicInfo info = {mosaicLastPoint, getAvarageColor(mosaicLastPoint)};
                         mosaicPoints.push_back(info);
                     }

                     if(diffY != 0){
                         mosaicLastPoint.ry() += mosaicWidth;

                         MosaicInfo info = {mosaicLastPoint, getAvarageColor(mosaicLastPoint)};
                         mosaicPoints.push_back(info);
                     }
                 }else if(endPoint.y() - mosaicLastPoint.y() < 0){
                     while(diffY > mosaicWidth){
                         mosaicLastPoint.ry() -= mosaicWidth;
                         diffY -= mosaicWidth;

                         MosaicInfo info = {mosaicLastPoint, getAvarageColor(mosaicLastPoint)};
                         mosaicPoints.push_back(info);
                     }

                     if(diffY != 0){
                         mosaicLastPoint.ry() -= mosaicWidth;

                         MosaicInfo info = {mosaicLastPoint, getAvarageColor(mosaicLastPoint)};
                         mosaicPoints.push_back(info);
                     }
                 }
             }
        }
        else if(isdrawrectangle||isdrawround||isdrawarrow ||istextedit)
        {
            endPoint=point;
        }

        updateNow();
    }else if(isdrawarrow2){
        QPoint point = event->pos();
        endPoint=point;
        updateNow();
    }

    event->ignore();
    QWidget::mouseMoveEvent(event);
}

void ImageEditer::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        ispressed = true;

        startPoint=event->pos();
        endPoint=event->pos();

        if(isclip){
            //啥也不干
        }
        else if(istextedit)
        {
            if(m_plaintextedit->toPlainText().size())
            {
                myText *text=new myText;
                text->color = currentColor();
                m_plaintextedit->setTextColor(text->color);

                text->mRect=QRect(QPoint(m_plaintextedit->x(),m_plaintextedit->y()),QSize(m_plaintextedit->width(),m_plaintextedit->height()));
                text->mText=m_plaintextedit->toPlainText();
                text->size = 8 + 5  * currentSize();
                addItem(text);
                updateNow();
            }

            m_plaintextedit->clear();
            m_plaintextedit->setTextColor(currentColor());
            m_plaintextedit->setFontPointSize(8 + 5 * currentSize());
            m_plaintextedit->setGeometry(startPoint.x(),startPoint.y(),80,30);
            ontextchanged();
            m_plaintextedit->show();
            m_plaintextedit->setFocus();
        }else if(isdrawline){
            myLine *line=new myLine;
            line->color =  currentColor();
            line->path.moveTo(startPoint);
            line->size = currentSize();
            addItem(line);
        }else if(isdrawmosaic) {
            mosaicLastPoint = event->pos();
            MosaicInfo info = {mosaicLastPoint, selectimage.pixelColor(mosaicLastPoint)};
            mosaicPoints.push_back(info);
        }else if(isdrawface) {
            myFace* face = new myFace;
            face->pos = startPoint;
            face->url = ":/image/face.png";
            addItem(face);
        }
    }

    event->ignore();
    QWidget::mouseMoveEvent(event);
}

void ImageEditer::addItem(BaseItem* itemPtr)
{
    items.push_back(BaseItemPtr(itemPtr));
    emit undoEnableChanged(true);
}

void ImageEditer::doClip()
{
    isclip = false;

    //重新设定位置及大小
    QPoint finalPoint =  this->mapToGlobal(clipRect.topLeft());
    QRect finalRect(finalPoint,QSize(clipRect.width(),clipRect.height()));

    emit cliped(finalRect);
}

void ImageEditer::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        ispressed = false;

        if(isclip){
            endPoint=event->pos();

            int sx = startPoint.x();
            int sy = startPoint.y();
            int ex = endPoint.x();
            int ey = endPoint.y();

            QPoint topLeft(qMin(sx,ex),qMin(sy,ey));
            QPoint bottomRight(qMax(sx,ex),qMax(sy,ey));

            clipRect = QRect(topLeft,bottomRight);

            int x = clipRect.right()- 95;
            int y = clipRect.bottom() + 5;

            if(x < 10){
                x = 10;
            }

            if(y + 36 > this->height()){
                y = clipRect.top() - 40;
            }

            clipConfirm->setGeometry(x,y,90, 36);
            clipConfirm->show();
        }
        else if(isdrawrectangle)
        {
            endPoint=event->pos();
            myRectangle *rectangle=new myRectangle;
            rectangle->color = currentColor();
            rectangle->size  = currentSize();
            rectangle->startPoint=startPoint;
            rectangle->endPoint=endPoint;
             addItem(rectangle);
        }
        else if(isdrawround)
        {
            endPoint=event->pos();
            myRound *round=new myRound;
            round->color = currentColor();
            round->size = currentSize();
            round->startPoint=startPoint;
            round->endPoint=endPoint;
            addItem(round);
        }
        else if(isdrawarrow)
        {
            endPoint=event->pos();

            int diffX = qAbs(endPoint.x() - startPoint.x());
            int diffY = qAbs(endPoint.y()- startPoint.y());

            qreal diff = qSqrt(diffX * diffX + diffY * diffY);
            if(diff > ARROW_MINSIZE){
                myArrow *arrow=new myArrow;
                arrow->color = currentColor();
                arrow->size = currentSize();
                arrow->startPoint=startPoint;
                arrow->endPoint=endPoint;
                addItem(arrow);
            }
        }else if(isdrawarrow2){
            endPoint = event->pos();

            BaseItemPtr lastItem;
            if(items.size()){
                lastItem = items.at(items.size()-1);

                if(lastItem && lastItem->type() == IT_ARROW2 ){
                    auto arrow2 = qSharedPointerCast<myArrow2>(lastItem);
                    if(arrow2 && !arrow2->closed){
                        arrow2->points.append(endPoint);
                    }else{
                        lastItem = BaseItemPtr();
                    }
                }else{
                    lastItem = BaseItemPtr();
                }
            }

            if(!lastItem){
                myArrow2 *arrow=new myArrow2;
                arrow->color = currentColor();
                arrow->size = currentSize();
                arrow->points.append(startPoint);
                arrow->points.append(endPoint);
                addItem(arrow);
            }
        }
        else if(isdrawmosaic){
            myMosaic* mosaic = new myMosaic;
            mosaic->mosaicWidth = getMosaicWidth();
            mosaic->vecMosaic = mosaicPoints;
            addItem(mosaic);
            mosaicPoints.clear();
            update();
        }

        updateNow();
    }else if(event->button() == Qt::RightButton){ //右键
        if(isdrawarrow2){
            endPoint = event->pos();

            BaseItemPtr lastItem;
            if(items.size()){
                lastItem = items.at(items.size()-1);

                if(lastItem && lastItem->type() == IT_ARROW2 ){
                    auto arrow2 = qSharedPointerCast<myArrow2>(lastItem);
                    if(arrow2 && !arrow2->closed){
                        arrow2->closed = true;

                        updateNow();
                    }
                }
            }
        }
    }

    event->ignore();
    QWidget::mouseMoveEvent(event);
}


void ImageEditer::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawImage(this->rect(),selectimage); //表示是在selectimage上面做编辑

    //绘制用户绘图
    for(int i=0;i<items.size(); ++i){
        items.at(i)->paint(painter);
    }

    if(ispressed){
        QPen pen;
        pen.setColor(currentColor());
        pen.setWidth(currentSize());
        painter.setPen(pen);
        int xx = startPoint.x() < endPoint.x() ? startPoint.x() : endPoint.x();
        int yy = startPoint.y() < endPoint.y() ? startPoint.y() : endPoint.y();

        if (isdrawrectangle)
        {
            painter.drawRect(xx, yy, abs(endPoint.x() - startPoint.x()), abs(endPoint.y() - startPoint.y()));
        }
        else if (isdrawround)
        {
            painter.drawEllipse(xx, yy, abs(endPoint.x() - startPoint.x()), abs(endPoint.y() - startPoint.y()));
        }
        else if(isdrawarrow)
        {
           painter.save();
           painter.setPen(currentColor());
           painter.setBrush(QBrush(currentColor()));
           drawarrow(startPoint,endPoint,painter);
           painter.restore();
        }
        else if(isdrawmosaic){
            painter.save();
            int width = getMosaicWidth();
            drawMosaic(width, mosaicPoints, painter);
            painter.restore();
        }
    }

    if(isdrawarrow2)
    {
        painter.save();
        QPen pen;
        pen.setColor(currentColor());
        pen.setWidth(currentSize());
        painter.setPen(pen);
        painter.setBrush(QBrush(currentColor()));

        BaseItemPtr lastItem;
        if(items.size()){
            lastItem = items.at(items.size()-1);

            if(lastItem && lastItem->type() == IT_ARROW2 ){
                auto arrow2 = qSharedPointerCast<myArrow2>(lastItem);
                if(arrow2 && !arrow2->closed){
                    auto lastPoint = arrow2->points.at(arrow2->points.size()-1);
                    drawarrow(lastPoint,endPoint,painter);
                }else{
                    lastItem = BaseItemPtr();
                }
            }else{
                lastItem = BaseItemPtr();
            }
        }

        if(!lastItem && ispressed){
            drawarrow(startPoint,endPoint,painter);
        }

        painter.restore();
    }

    if(isclip && !clipRect.isEmpty()){
        QPainterPath parentPath,subPath;
        parentPath.addRect(this->rect());
        subPath.addRect(clipRect);
        QPainterPath path = parentPath.subtracted(subPath);

        painter.save();

        QPen pen;
        pen.setColor(QColor(74,76,91));
        pen.setWidth(2);
        painter.setPen(pen);
        painter.setBrush(QColor(0, 0, 0, 100));

        painter.drawPath(path);
        painter.restore();
    }
}

bool ImageEditer::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == m_plaintextedit &&  event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if(keyEvent->key() == Qt::Key_Delete){
            m_plaintextedit->clear();
            m_plaintextedit->hide();
            return true;
        }
    }else if(qobject_cast<QPushButton*>(watched) && event->type() == QEvent::Enter){
        setCursor(Qt::PointingHandCursor);
        return true;
    }else if(qobject_cast<QPushButton*>(watched) && event->type() == QEvent::Leave){
        setCursor(Qt::CrossCursor);
        return true;
    }

    return QObject::eventFilter(watched, event);
}

void ImageEditer::setimagetolabel(const QImage &image)
{
    selectimage=image;

    items.clear(); //清楚绘制项目

    endPoint=QPoint(0,0);
    startPoint=QPoint(0,0);

    updateNow();
}

void ImageEditer::undo()
{
    if(items.size()>0){
        items.pop_back();
        this->updateNow();
        emit undoEnableChanged(items.size()>0);
    }
}

void ImageEditer::setdrawlineenable()
{
    settextedittovector();

    isdrawline=true;
    isdrawrectangle=false;
    isdrawround=false;
    istextedit=false;
    isdrawarrow=false;
    isdrawmosaic=false;
    isdrawface = false;
    isdrawarrow2 =false;

    m_plaintextedit->clear();
    m_plaintextedit->hide();
}

void ImageEditer::setrectangleenable()
{
    settextedittovector();
    isdrawline=false;
    isdrawrectangle=true;
    isdrawround=false;
    istextedit=false;
    isdrawarrow=false;
    isdrawmosaic=false;
    isdrawface = false;
    isdrawarrow2 =false;

    m_plaintextedit->clear();
    m_plaintextedit->hide();
}

void ImageEditer::setdrawarrowenable()
{
    settextedittovector();
    isdrawline=false;
    isdrawarrow=true;
    isdrawrectangle=false;
    isdrawround=false;
    istextedit=false;
    isdrawmosaic=false;
    isdrawface = false;
    isdrawarrow2 =false;

    m_plaintextedit->clear();
    m_plaintextedit->hide();
}

void ImageEditer::setdrawarrow2enable()
{
    settextedittovector();
    isdrawline=false;
    isdrawarrow=false;
    isdrawrectangle=false;
    isdrawround=false;
    istextedit=false;
    isdrawmosaic=false;
    isdrawface = false;
    isdrawarrow2 = true;

    m_plaintextedit->hide();
}

void ImageEditer::setroundenable()
{
    settextedittovector();
    isdrawline=false;
    isdrawrectangle=false;
    isdrawround=true;
    isdrawarrow=false;
    istextedit=false;
    isdrawmosaic=false;
    isdrawface = false;
    isdrawarrow2 =false;

    m_plaintextedit->clear();
    m_plaintextedit->hide();
}

void ImageEditer::settexteditenable()
{
    settextedittovector();
    isdrawline=false;
    isdrawrectangle=false;
    isdrawround=false;
    isdrawarrow=false;
    istextedit=true;
    isdrawmosaic=false;
    isdrawface = false;
    isdrawarrow2 =false;

    m_plaintextedit->clear();
    m_plaintextedit->hide();
}

void ImageEditer::settextedittovector()
{
    if (/*istextedit &&*/ m_plaintextedit->isVisible() &&  m_plaintextedit->toPlainText().size())
    {
        myText *text = new myText;
        text->color = m_plaintextedit->textColor();
        text->size =  m_plaintextedit->fontPointSize();
        text->mRect = QRect(QPoint(m_plaintextedit->x(), m_plaintextedit->y()), QSize(m_plaintextedit->width(), m_plaintextedit->height()));
        text->mText = m_plaintextedit->toPlainText();
         addItem(text);
        updateNow();
    }
}

void ImageEditer::setdrawmosaicnable()
{
    settextedittovector();

    isdrawline=false;
    isdrawrectangle=false;
    isdrawround=false;
    istextedit=false;
    isdrawarrow=false;
    isdrawmosaic = true;
    isdrawface = false;
    isdrawarrow2 =false;

    m_plaintextedit->clear();
    m_plaintextedit->hide();
}

void ImageEditer::setdrawfacenable()
{
    settextedittovector();

    isdrawline=false;
    isdrawrectangle=false;
    isdrawround=false;
    istextedit=false;
    isdrawarrow=false;
    isdrawmosaic = false;
    isdrawface = true;
    isdrawarrow2 =false;

    m_plaintextedit->clear();
    m_plaintextedit->hide();
}

void ImageEditer::setclipable()
{
    settextedittovector();

    isclip = true;

    m_plaintextedit->clear();
    m_plaintextedit->hide();
}

bool ImageEditer::isEditing() const
{
    return ( isdrawarrow ||
                isdrawline ||
                isdrawrectangle ||
                isdrawround ||
                istextedit ||
                isdrawarrow ||
             isdrawarrow2 ||
             isdrawmosaic||
             isdrawface ||
             isclip);
}

QPixmap ImageEditer::resultimage()
{
    settextedittovector();

    QImage img = this->HDResultImage(selectimage);
    return QPixmap::fromImage(img);
    //return grab();
}

QImage ImageEditer::HDResultImage(QImage img){
    settextedittovector();

    QImage result_image_v = img;
    QPainter painter;
    painter.begin(&result_image_v);
    painter.setRenderHint(QPainter::Antialiasing, true);

    QRect rect = this->rect();
    QRect imgRect = img.rect();

    qreal scale = (qreal)imgRect.width()/rect.width();

    painter.scale(scale,scale);

    //绘制用户绘图
    for(int i=0;i<items.size(); ++i){
        items.at(i)->paint(painter);
    }

    return result_image_v;
}

void ImageEditer::updateNow()
{
    this->update();
}

void ImageEditer::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        emit doubleClicked();
    }
}

void myLine::paint(QPainter &painter)
{
    painter.save();
    QPen pen;
    pen.setColor(QColor(color));
    pen.setWidth(size);
    painter.setPen(pen);
    //painter.drawLine(startPoint,endPoint);
    painter.drawPath(path);
    painter.restore();
}

void myRectangle::paint(QPainter &painter)
{
    int x1 = startPoint.x() < endPoint.x() ? startPoint.x() : endPoint.x();
        int y1 = startPoint.y() < endPoint.y() ? startPoint.y() : endPoint.y();
        painter.save();
        QPen pen;
        pen.setColor(QColor(color));
        pen.setWidth(size);
        painter.setPen(pen);
    painter.drawRect(x1, y1, abs(endPoint.x() - startPoint.x()), abs(endPoint.y() - startPoint.y()));
    painter.restore();
}

void myRound::paint(QPainter &painter)
{
    int x2 = startPoint.x() < endPoint.x() ? startPoint.x() : endPoint.x();
    int y2 = startPoint.y() < endPoint.y() ? startPoint.y() : endPoint.y();
    painter.save();
    QPen pen;
    pen.setColor(QColor(color));
    pen.setWidth(size);
    painter.setPen(pen);
    painter.drawEllipse(x2, y2, abs(endPoint.x() - startPoint.x()), abs(endPoint.y() - startPoint.y()));
}

void myArrow::paint(QPainter &painter)
{
    painter.save();

    painter.setPen(QPen(color));
    painter.setBrush(QBrush(color));
    drawarrow(startPoint,endPoint,painter);
    painter.restore();
}

void myText::paint(QPainter &painter)
{
    painter.save();

    QFont font = painter.font();
    font.setPointSize(size);
    painter.setFont(font);
    QPen pen;
    pen.setColor(color);
    //pen.setWidth(size);;

    painter.setPen(pen);
    auto rect = mRect;
    rect.setTopLeft(QPoint(mRect.x()+5,mRect.y()+5));
    rect.setWidth(mRect.width());
    rect.setHeight(mRect.height());
    //painter.drawText(mRect.topLeft(),mText);
    painter.drawText(rect, Qt::TextWrapAnywhere, mText);
    painter.restore();
}

void myMosaic::paint(QPainter &painter){
    painter.save();

    drawMosaic(mosaicWidth, vecMosaic, painter);

    painter.restore();
}

void myFace::paint(QPainter &painter)
{
    painter.save();
    QImage img(url);
    QPoint destPos(pos.x()-img.width()/2,pos.y()-img.height()/2);
    painter.drawImage(destPos,QImage(url));
    painter.restore();
}

void myArrow2::paint(QPainter &painter)
{
    painter.save();

    QPen pen;
    pen.setColor(color);
    pen.setWidth(size);;
    painter.setPen(pen);
    painter.setBrush(QBrush(color));

    //if(closed){
    drawarrow2(points,painter);
    //    }else{
    //        for(int i=0;i<points.size()-1;++i){
    //            auto p1 = points.at(i);
    //            auto p2 = points.at(i+1);
    //            painter.drawLine(p1,p2);
    //        }
    //    }

    painter.restore();
}
